/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * actionPanel.java
 *
 * Created on 2011-mar-02, 14:54:52
 */

package imat;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.SwingConstants;
import javax.swing.Timer;
import se.chalmers.ait.dat215.project.IMatDataHandler;
import se.chalmers.ait.dat215.project.ShoppingCart;

/**
 *
 * @author Harryson
 */
public class actionPanel extends javax.swing.JPanel {

    private IMatPresenter presenter  = IMatPresenter.getInstance();

    private int           blinkCount = 0;
    private Color         blinkColor = Color.WHITE;
    private Timer         blinkTimer =
        new Timer(300,
                  new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        apBasketButton.setForeground(blinkColor);

                        if(blinkColor == Color.WHITE) {
                            blinkColor = Color.BLACK;
                        }
                        else {
                            blinkColor = Color.WHITE;
                            blinkCount++;
                        }

                        if(blinkCount >= 2) {
                            blinkTimer.stop();
                            blinkCount = 0;
                        }
                    }
                  }
    );

    /** Creates new form actionPanel */
    public actionPanel() {
        initComponents();

        apBasketButton.setVerticalTextPosition(SwingConstants.CENTER);
        apBasketButton.setHorizontalTextPosition(SwingConstants.CENTER);

        ShoppingCart cart = IMatDataHandler.getInstance().getShoppingCart();
        updateCartButtonText(cart.getItems().size(), cart.getTotal());
    }

    public void updateCartButtonText(int nrOfItems, double sum) {       
        apBasketButton.setText("<html><p><p><p><p>Antal varor: " + nrOfItems + " st<p>Summa: " + presenter.doublePad(sum) + " kr</p></p></p></p></p></html>");
        blinkTimer.start();
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        actionPanel1 = new javax.swing.JPanel();
        apBasketButton = new javax.swing.JButton();
        apUserinfoButton = new javax.swing.JButton();
        apReceiptButton = new javax.swing.JButton();
        apCashregisterButton = new javax.swing.JButton();

        setName("Form"); // NOI18N

        actionPanel1.setName("actionPanel1"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(imat.IMatApp.class).getContext().getResourceMap(actionPanel.class);
        apBasketButton.setFont(resourceMap.getFont("apBasketButton.font")); // NOI18N
        apBasketButton.setIcon(resourceMap.getIcon("apBasketButton.icon")); // NOI18N
        apBasketButton.setText(resourceMap.getString("apBasketButton.text")); // NOI18N
        apBasketButton.setToolTipText(resourceMap.getString("apBasketButton.toolTipText")); // NOI18N
        apBasketButton.setName("apBasketButton"); // NOI18N
        apBasketButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                apBasketButtonActionPerformed(evt);
            }
        });

        apUserinfoButton.setIcon(resourceMap.getIcon("apUserinfoButton.icon")); // NOI18N
        apUserinfoButton.setText(resourceMap.getString("apUserinfoButton.text")); // NOI18N
        apUserinfoButton.setToolTipText(resourceMap.getString("apUserinfoButton.toolTipText")); // NOI18N
        apUserinfoButton.setName("apUserinfoButton"); // NOI18N
        apUserinfoButton.setPreferredSize(new java.awt.Dimension(200, 65));
        apUserinfoButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                apUserinfoButtonActionPerformed(evt);
            }
        });

        apReceiptButton.setIcon(resourceMap.getIcon("apReceiptButton.icon")); // NOI18N
        apReceiptButton.setText(resourceMap.getString("apReceiptButton.text")); // NOI18N
        apReceiptButton.setToolTipText(resourceMap.getString("apReceiptButton.toolTipText")); // NOI18N
        apReceiptButton.setName("apReceiptButton"); // NOI18N
        apReceiptButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                apReceiptButtonActionPerformed(evt);
            }
        });

        apCashregisterButton.setIcon(resourceMap.getIcon("apCashregisterButton.icon")); // NOI18N
        apCashregisterButton.setText(resourceMap.getString("apCashregisterButton.text")); // NOI18N
        apCashregisterButton.setToolTipText(resourceMap.getString("apCashregisterButton.toolTipText")); // NOI18N
        apCashregisterButton.setName("apCashregisterButton"); // NOI18N
        apCashregisterButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                apCashregisterButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout actionPanel1Layout = new javax.swing.GroupLayout(actionPanel1);
        actionPanel1.setLayout(actionPanel1Layout);
        actionPanel1Layout.setHorizontalGroup(
            actionPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(actionPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(actionPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(actionPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(apReceiptButton, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                        .addComponent(apUserinfoButton, javax.swing.GroupLayout.PREFERRED_SIZE, 210, Short.MAX_VALUE))
                    .addGroup(actionPanel1Layout.createSequentialGroup()
                        .addGroup(actionPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(apCashregisterButton, 0, 0, Short.MAX_VALUE)
                            .addComponent(apBasketButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 207, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        actionPanel1Layout.setVerticalGroup(
            actionPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, actionPanel1Layout.createSequentialGroup()
                .addComponent(apBasketButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 68, Short.MAX_VALUE)
                .addComponent(apUserinfoButton, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(apReceiptButton, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(35, 35, 35)
                .addComponent(apCashregisterButton)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(actionPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(actionPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void apBasketButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_apBasketButtonActionPerformed
        presenter.displayCategory(IMatView.CART);
    }//GEN-LAST:event_apBasketButtonActionPerformed

    private void apUserinfoButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_apUserinfoButtonActionPerformed
        IMatPresenter.getInstance().getView().getUserInfoView1().initUserInfo();
        presenter.displayCategory(IMatView.USERINFO);
    }//GEN-LAST:event_apUserinfoButtonActionPerformed

    private void apCashregisterButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_apCashregisterButtonActionPerformed
        IMatPresenter.getInstance().getView().getCashRegister1().initCashUserInfo();
        presenter.displayCategory(IMatView.CASH);
    }//GEN-LAST:event_apCashregisterButtonActionPerformed

    private void apReceiptButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_apReceiptButtonActionPerformed
        IMatPresenter.getInstance().getView().getPurchases1().displayPreviousPurchases();
        presenter.displayCategory(IMatView.PURCHASES);
    }//GEN-LAST:event_apReceiptButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel actionPanel1;
    private javax.swing.JButton apBasketButton;
    private javax.swing.JButton apCashregisterButton;
    private javax.swing.JButton apReceiptButton;
    private javax.swing.JButton apUserinfoButton;
    // End of variables declaration//GEN-END:variables

}
